package cn.shangqizhenshuai.sqiot.action;

import cn.shangqizhenshuai.sqiot.Serivce.EquipmentService;
import cn.shangqizhenshuai.sqiot.entity.EA;
import cn.shangqizhenshuai.sqiot.entity.EAExample;
import cn.shangqizhenshuai.sqiot.entity.Equipment;
import cn.shangqizhenshuai.sqiot.entity.EquipmentExample;
import cn.shangqizhenshuai.sqiot.mapper.AreaMapper;
import cn.shangqizhenshuai.sqiot.mapper.EAMapper;
import cn.shangqizhenshuai.sqiot.mapper.EquipmentMapper;
import cn.shangqizhenshuai.sqiot.mqtt.Mqttservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ResponseBody
@Controller
@RequestMapping("equipment")
public class EquipmentAction {
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private Mqttservice mqttservice;

    /**
     * 根据id 查设备
     * @param id
     * @return
     */
    @RequestMapping("/getlistbyareaid")
    @ResponseBody
    public List<Equipment> getlistbyareaid(int id){
        HashMap<String, Object> stringBooleanHashMap = new HashMap<String, Object>();
        List<Equipment> list_by_area_id = equipmentService.get_list_by_area_id(id);
        stringBooleanHashMap.put("data",list_by_area_id);
        stringBooleanHashMap.put("errorcode","00000");
        return list_by_area_id;
    }

    /**
     * 根据主键修改 是否启动
     * @param id 设备id
     * @param flag 0启用1不启用
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateflagbyid")
    public Map<String, Object> updateflagbyid(int id,int flag){
        int i =0;
         i = equipmentService.update_flag_by_id(id, flag);
        HashMap<String, Object> stringBooleanHashMap = new HashMap<String, Object>();
        if ((i == 0)) {
            stringBooleanHashMap.put("data", false);
        } else {
            stringBooleanHashMap.put("data", true);
        }
        stringBooleanHashMap.put("errorcode","00000");
        return stringBooleanHashMap;
    }

    /**
     *  根据主键修改设备状态
     * @param id
     * @param message
     * @param qos
     * @return
     */
    @ResponseBody
    @RequestMapping("/setequipmentvaluetomqtt")
    public Map setequipmentvaluetomqtt(int id,String message,int qos){
        HashMap<String, Object> stringBooleanHashMap = new HashMap<String, Object>();
        //获取设备信息
        Equipment by_id = equipmentService.get_by_id(id);
        //获取他的设置连接
        String seturl = by_id.getSeturl();
        if(mqttservice.pub(seturl,message,qos)){
            stringBooleanHashMap.put("data",true);
        }else{
            stringBooleanHashMap.put("data",false);
        }
        stringBooleanHashMap.put("errorcode","00000");
        return stringBooleanHashMap;
    }

    /**
     *  最新状态值
     * @param eids 需要查询的设备id集合
     * @return id和值 当值为0.00时(double)则 该设备关闭
     */
    @ResponseBody
    @RequestMapping("/getstatevalue")
    public Map getstatevalue(String eids){
        HashMap<String, Object> stringBooleanHashMap = new HashMap<String, Object>();
        //获取最新值
        Map<Integer, Double> r_value = equipmentService.get_r_value(eids);
        stringBooleanHashMap.put("data",r_value);
        return stringBooleanHashMap;
    }



}
